<template>
  <div class="rotate-group">
    <div id="scene"></div>
  </div>
</template>

<script>
import * as THREE from "three";
import { OrbitControls } from "three/examples/jsm/controls/OrbitControls";
import TWEEN from "tween.js";

let scene; //场景
let camera; //相机
let renderer; //创建渲染器
let controls; //控制器

export default {
  name: "rotateGroup",
  data() {
    return {};
  },
  mounted() {
    this.createScene();
    this.createMesh();
    this.createLight();
    this.createCamera();
    this.createRender();
    this.render();
    this.createControls();
  },
  methods: {
    createScene() {
      //创建场景
      scene = new THREE.Scene();

      scene.add(new THREE.AxesHelper(20));
    },
    // 创建网格模型
    createMesh() {
      const sGeometry = new THREE.SphereGeometry(1, 50, 50);
      const sMaterial = new THREE.MeshPhongMaterial({ color: 0x555555 });
      this.sphere = new THREE.Mesh(sGeometry, sMaterial);
      scene.add(this.sphere);

      // twwen代码设置
      this.positionX = { x: 0, y: 0 };
      const tween = new TWEEN.Tween(this.positionX).to({ x: 1, y: 1 }, 3000);
      tween.easing(TWEEN.Easing.Sinusoidal.InOut);
      tween.start();

      const tweenBack = new TWEEN.Tween(this.positionX).to(
        { x: 0, y: 0 },
        3000
      );
      tweenBack.easing(TWEEN.Easing.Sinusoidal.InOut);

      tween.chain(tweenBack);
      tweenBack.chain(tween);

      tween.onUpdate(this.onUpdate);
      tweenBack.onUpdate(this.onUpdate);
    },
    onUpdate() {
      this.sphere.position.x = this.positionX.x;
      this.sphere.position.y = this.positionX.y;
    },
    createLight() {
      // 点光源
      const point = new THREE.PointLight(0xffffff);
      // 点光源位置
      point.position.set(400, 200, 300);
      // 点光源添加到场景
      scene.add(point);

      // 环境光
      const ambient = new THREE.AmbientLight(0x666666);
      scene.add(ambient);
    },
    createCamera() {
      const element = document.getElementById("scene");
      camera = new THREE.PerspectiveCamera(
        35,
        element.clientWidth / element.clientHeight,
        0.1,
        1000
      );
      camera.position.set(0, 5, 8); // 设置相机位置

      camera.lookAt(scene.position);
    },
    // 创建渲染器
    createRender() {
      const element = document.getElementById("scene");
      renderer = new THREE.WebGLRenderer();
      // 设置渲染区域尺寸
      renderer.setSize(element.clientWidth, element.clientHeight);
      // 设置背景色
      renderer.setClearColor(0x3f3f3f, 1);
      element.appendChild(renderer.domElement);
    },
    render() {
      TWEEN.update();
      renderer.render(scene, camera);
      // 通过requestAnimationFrame实现周期性调用render
      requestAnimationFrame(this.render);
    },
    createControls() {
      controls = new OrbitControls(camera, renderer.domElement);
    },
  },
};
</script>

<style scoped lang="less">
.rotate-group {
  width: 100%;
  height: 100%;
  position: relative;
}

.open-btn {
  position: absolute;
  top: 20px;
  left: 20px;
}
#scene {
  width: 100%;
  height: 100%;
}
</style>
